﻿
namespace SaveTheQueen.Utils.Colision
{
    public class PermutationAccel : BaseAccelerator
    {
        private int currentNode = 0; // Current and pair are the indexes of the permutation.
        private int pairNode = 0;

        public PermutationAccel() : base() { }

        public override void NextPair(ColisionManager receiver)
        {
            if (pairNode < receiver.Nodes.Count - 1)
            {
                pairNode++;
                receiver.CheckPair(receiver.Nodes[currentNode], receiver.Nodes[pairNode]);
            }
            else
            {
                if (currentNode < receiver.Nodes.Count - 1)
                {
                    currentNode++;
                    pairNode = currentNode + 1; // avoid re checking of some pairs.
                    if (pairNode < receiver.Nodes.Count)
                    {
                        receiver.CheckPair(receiver.Nodes[currentNode], receiver.Nodes[pairNode]);
                    }
                    else
                    {   // null, null will terminate the cheking process.
                        receiver.CheckPair(null, null);
                        return;
                    }
                }
                else
                {   // null, null will terminate the cheking process.
                    receiver.CheckPair(null, null);
                    return;
                }
            }
        }

        public override void End()
        {
            currentNode = 0;
            pairNode = 0;
            base.End();
        }
    }
}
